import {
  getCustomerTable
} from '../../api/customer'
import {
  SALE_DRAWER_LIST,
  SALE_DRAWER_CURRENT_ROW,
  SALE_DRAWER_DATA_COUNT,
  SALE_DRAWER_HISTORY_DATA,
  SALE_DRAWER_PAGE_SIZE
} from '../motations-type'

export default {
  state: {
    saleDrawerList: [],
    saleDrawerDataCount: null,
    saleDrawerPageSize: 10,
    saleDrawerHistoryData: [],
    row: {
      uid: '',
      ctime: '',
      uname: '',
      tel: '',
      coludname: '',
      coludtel: '',
      mname: '',
      appointime: '',
      state: '',
      comdate: '',
      tname: '',
      gid: '',
      gname: '',
      orderdate: '',
      cprice: '',
      commission: '',
      state0: '',
      ttype: '',
      remarks: ''
    }
  },
  getters: {
    saleDrawerList: state => state.saleDrawerList
  },
  mutations: {
    [SALE_DRAWER_LIST] (state, data) {
      state.saleDrawerList = data
    },
    [SALE_DRAWER_CURRENT_ROW] (state, data) {
      state.row = data
    },
    [SALE_DRAWER_DATA_COUNT] (state, data) {
      state.saleDrawerDataCount = data
    },
    [SALE_DRAWER_HISTORY_DATA] (state, data) {
      state.saleDrawerHistoryData = data
    },
    [SALE_DRAWER_PAGE_SIZE] (state, data) {
      state.saleDrawerPageSize = data
    }
  },
  actions: {
    async getSaleDrawerList ({ commit, rootState }, playload) {
      let data = {}
      let newData = []
      if (Object.keys(playload).length > 0) {
        data = playload
      }
      const resp = await getCustomerTable(data)
      resp.data.data.rows.map((item) => {
        newData = [...newData, {
          id: item.id,
          uid: item.uid,
          ctime: item.ctime,
          uname: item.uname,
          tel: item.tel,
          coludname: item.coludname,
          coludtel: item.coludtel,
          mname: item.mname,
          appointime: item.appointime,
          state: item.state.toString(),
          comdate: item.comdate,
          tname: item.tname,
          gid: item.gid,
          gname: item.gname,
          orderdate: item.orderdate,
          cprice: item.cprice,
          commission: item.commission,
          state0: item.state0,
          ttype: item.ttype ? item.ttype.toString() : '',
          remarks: item.remarks
        }]
      })
      commit(SALE_DRAWER_LIST, newData)
      commit(SALE_DRAWER_DATA_COUNT, newData.length)
      if (newData.length < rootState.saleDrawerTable.saleDrawerPageSize) {
        commit(SALE_DRAWER_HISTORY_DATA, newData)
      } else {
        commit(SALE_DRAWER_HISTORY_DATA, newData.slice(0, rootState.saleDrawerTable.saleDrawerHistoryData))
      }
    },
    async getSaleDrawerCurrentRow ({ commit, rootState }, row) {
      commit(SALE_DRAWER_CURRENT_ROW, row)
    },
    async changeSaleDrawerPage ({ commit, rootState }, index) {
      let data = rootState.saleDrawerTable.saleDrawerList
      let _start = (index - 1) * rootState.saleDrawerTable.saleDrawerPageSize
      let _end = index * rootState.saleDrawerTable.saleDrawerPageSize
      commit(SALE_DRAWER_HISTORY_DATA, data.slice(_start, _end))
    },
    async changeSaleDrawerPageSize ({ commit, rootState }, value) {
      commit(SALE_DRAWER_PAGE_SIZE, value)
    }
  }
}
